import { reqRegister } from "../../http/api"
import { Toast } from "antd-mobile"
//初始值
const initState={
    user: {
        phone: "",
        nickname: "",
        password: "",
      },
}

//action types
const types={
    REG_CHANGE_USER:"REG_CHANGE_USER"
}


//action creator
export const actions={
    changeUser:(key,value)=>({
        type:types.REG_CHANGE_USER,
        key,
        value
    }),
    // 注册逻辑
    registerAction:(push)=>(dispatch,getState)=>{
        let{
            user
        } = getState().register
        reqRegister(user).then(res=>{
            if(res.data.code===200){
                Toast.info(res.data.msg)
                push("/login")
            }
        })
    }
}


//reducer
export const RegReducer=(state=initState,action)=>{
    switch (action.type) {
        case types.REG_CHANGE_USER:
            return{
                ...state,
                user:{
                    ...state.user,
                    [action.key]:action.value
                }
            }
            break;
        default:
        return state;
    }
}

//导出数据
export const getUser = state => state.register.user;
